﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class UserArea : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            foreach (HtmlGenericControl menu in menustripholder1.Controls.OfType<HtmlGenericControl>())
                if (menu.TagName == "ul" && menu.Attributes["class"] == "menustrip")
                    foreach (HtmlGenericControl menuitem in menu.Controls.OfType<HtmlGenericControl>())
                        if (menuitem.TagName == "li")
                            foreach (HtmlAnchor menuitemlink in menuitem.Controls.OfType<HtmlAnchor>())
                                if (Request.Url.AbsolutePath.ToLower().Contains("/" + menuitemlink.HRef.ToLower()))
                                    menuitemlink.Attributes.Add("class", "selectedmenuitem");
            foreach (HtmlGenericControl menu in menustripholder2.Controls.OfType<HtmlGenericControl>())
                if (menu.TagName == "ul" && menu.Attributes["class"] == "menustrip")
                    foreach (HtmlGenericControl menuitem in menu.Controls.OfType<HtmlGenericControl>())
                        if (menuitem.TagName == "li")
                            foreach (HtmlAnchor menuitemlink in menuitem.Controls.OfType<HtmlAnchor>())
                                if (Request.Url.AbsolutePath.ToLower().Contains("/" + menuitemlink.HRef.ToLower()))
                                    menuitemlink.Attributes.Add("class", "selectedmenuitem");
        }

        private void Page_PreRender(object sender, EventArgs e)
        {
            DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
            DataAccessLayer.User User = null;
            int UserID = (int)Session["UserID"];
            User = DataAccessLayer.User.FindByUserID(db, UserID);
            lblOwnerName.Text = User.DisplayName;
            lblUsername.Text = User.Username;
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
            DataAccessLayer.User User = null;
            if (Session["UserID"] != null)
            {
                int UserID = (int)Session["UserID"];
                User = DataAccessLayer.User.FindByUserID(db, UserID);
            }
            if (!(User != null && User.Enable))
                Response.Redirect("~/Login.aspx?logout=1");
        }
    }
}